package com.gqzdev.xblog.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName SendMail
 * @Description
 * @Author ganquanzhong
 * @Date2020/2/26 21:49
 * @Version
 **/

@Component
public class SendMailUtil {

    @Autowired
    private JavaMailSender javaMailSender;

    public String send(String subject,String from,String to,String context) throws MessagingException {
        //mime邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true);
        messageHelper.setSubject(subject);
        messageHelper.setFrom(from);
        //抄送
        //messageHelper.setCc(from);
        messageHelper.setTo(to);
        //true表示text以html形式
        messageHelper.setText(context, true);
        //附件 图片文件等
        //messageHelper.addAttachment("补丁",new File("I:\\Tools\\IDE\\jetbrains-agent\\jetbrains-agent.jar"));
        javaMailSender.send(messageHelper.getMimeMessage());
        return "ok";
    }

}
